import { defineStore } from 'pinia';
import { reactive, ref, watch } from 'vue';
import { Attendances } from '@/api/sting/attendance';
import { ResultEnum } from '@/enums/httpEnum';

interface attendances {
  id: number;
  personId: string;
  startWork: string;
  offDuty: string;
}
export const AttendanceStore = defineStore('attend', () => {
  const attendes = ref<attendances[]>([]);
  const limit = ref(4),
    offset = ref(0),
    count = ref(0),
    max = ref(0),
    page = ref(1),
    conditions = reactive({
      days: 'all',
      attendance: 'all',
    });
  // 查询数据
  const search = async () => {
    const data = await Attendances({
      limit: limit.value,
      offset: (page.value - 1) * limit.value,
      ...conditions,
    });
    const { result } = data;
    console.log(result);
    console.log(data);

    if (data.code == ResultEnum.SUCCESS) {
      count.value = result.count;
      page.value = result.page;
      max.value = Math.ceil(count.value / limit.value);
      attendes.value = result;
    }
  };
  watch(
    conditions,
    () => {
      attendes.value = [];
      count.value = 0;
      page.value = 1;
      search();
    },
    { deep: true }
  );

  return {
    attendes,
    offset,
    limit,
    conditions,
    search,
    page,
    max,
    count,
  };
});
